package utilities;

import java.util.ArrayList;
import java.util.List;
import java.sql.Date;
import java.sql.*;

import javax.naming.*;
import javax.sql.*;

public class NoviceDAOImpl implements BaseDAO {
	Connection con = null; 
	@Override
	public Connection getConnection() {
		try { 
			Context initCtx = new InitialContext(); 
			// DataSource pridobimo glede na JNDI 
			DataSource ds = (DataSource)initCtx.lookup("jdbc/AlumniDS");
			ds.setLoginTimeout(10);
			con = ds.getConnection(); 
			
		} catch(Exception e) { 
			System.out.println("JDBC vir ne obstaja!"); 
			e.printStackTrace(); 
		} 
		return con;
	}

	@Override
	public Entiteta vrni(int id) {
		PreparedStatement stmt = null; 
		Entiteta novica = null;
		try { 
			String sql = "select * from NOVICE where id = ?"; 
			stmt = con.prepareStatement(sql);  
			stmt.setInt(1, id);
			ResultSet rs = stmt.executeQuery();  
			// obdelava rezultatov 
			if(rs.next()){ 
				int id_avtorja = rs.getInt("id_avtorja"); 
				String naziv = rs.getString("naziv"); 
				String vsebina = rs.getString("vsebina"); 
				Date datum = rs.getDate("datum_objave");
				// nadaljna obdelava rezultatov 
				novica = new Novica(id, id_avtorja, naziv, vsebina, datum);
			}else { 
				System.out.println("Ne najdem novic");
			} 
		} catch(SQLException e) { 
			e.printStackTrace(); 
		} finally { 
			if (stmt != null)
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} 
		}
		return novica;
	}

	@Override
	public void vstavi(Entiteta ent) {
		PreparedStatement stmt = null; 
		Novica novica = (Novica)ent;
		try { 
			String sql = "insert into NOVICE (id_avtorja, naziv, vsebina, datum_objave) values (?, ?, ?, ?)"; 
			stmt = con.prepareStatement(sql);  
			stmt.setInt(1, novica.getIdA());
			stmt.setString(2, novica.getNaziv());
			stmt.setString(3, novica.getVsebina());
			stmt.setDate(4, novica.getDatum());
			stmt.executeUpdate();
		} catch(SQLException e) { 
			e.printStackTrace(); 
		} finally { 
			if (stmt != null)
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} 
		}
		
	}

	@Override
	public void odstrani(int id) {
		PreparedStatement stmt = null; 
		try { 
			String sql = "delete from NOVICE where id = ?"; 
			stmt = con.prepareStatement(sql);  
			stmt.setInt(1, id);
			stmt.executeUpdate();
		} catch(SQLException e) { 
			e.printStackTrace(); 
		} finally { 
			if (stmt != null)
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} 
		}
		
	}

	public void posodobi(int id, Entiteta ent) {
		PreparedStatement stmt = null; 
		Novica novica = (Novica)ent;
		try { 
			String sql = "update NOVICE set naziv = ?, vsebina = ?, datum_objave = ? where id = ?"; 
			stmt = con.prepareStatement(sql);  
			stmt.setString(1, novica.getNaziv());
			stmt.setString(2, novica.getVsebina());
			stmt.setDate(3, novica.getDatum());
			stmt.setInt(4, id);
			stmt.executeUpdate();

		} catch(SQLException e) { 
			e.printStackTrace(); 
		} finally { 
			if (stmt != null)
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} 
		}
		
	}

	@Override
	public List<Entiteta> vrniVse() {
		Statement stmt = null; 
		ArrayList<Entiteta> novice = new ArrayList<Entiteta>();
		try { 
			stmt = con.createStatement(); 
			String sql = "select * from NOVICE"; 
			ResultSet rs = stmt.executeQuery(sql); 
			// obdelava rezultatov 
			while(rs.next()){		// Goran 27.11.2011 - tukaj mora biti zanka in ne IF stavek
				int id = rs.getInt("id");
				int id_avtorja = rs.getInt("id_avtorja"); 
				String naziv = rs.getString("naziv"); 
				String vsebina = rs.getString("vsebina"); 
				java.sql.Date datum = rs.getDate("datum_objave");
				// nadaljna obdelava rezultatov 
				Entiteta vnos = new Novica(id, id_avtorja, naziv, vsebina, datum);
				novice.add(vnos);
			}
		} catch(SQLException e) { 
			e.printStackTrace(); 
		} finally { 
			if (stmt != null)
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} 
		}
		return novice;
	}

	@Override
	public void posodobi(int id) {
		// TODO Auto-generated method stub
		
	}

}
